package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.CartM;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.adapter.BaseViewHolder;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;
import com.luck.easyrecyclerview.swipe.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class ShopCardAdapter extends RecyclerArrayAdapter<CartM.DataBean.CartsBean> {
    private Context mContext;
    private ShopCardItemAdapter adapter;
    private List<CartM.DataBean.CartsBean> list = new ArrayList<>();

    public OnCartNumber onCartNumber;

    public interface OnCartNumber {
        void onCartNumber(String cid, int num);
    }

    public OnRefresh onActivityRefresh;

    public interface OnRefresh {
        void onActivityRefresh(boolean isChecked, String cid, int position);
    }

    public ShopCardAdapter(Context context, List<CartM.DataBean.CartsBean> list, OnRefresh onActivityRefresh, OnCartNumber onCartNumber) {
        super(context);
        this.mContext = context;
        this.list = list;
        this.onActivityRefresh = onActivityRefresh;
        this.onCartNumber = onCartNumber;
    }

    public void setList(List<CartM.DataBean.CartsBean> list) {
        this.list = list;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

        return new CouponViewHolder(parent);
    }


    public class CouponViewHolder extends BaseViewHolder<CartM.DataBean.CartsBean> {
        ImageView iv_cb;
        TextView tv_shopName;
        RecyclerView recyclerView;
        LinearLayout ll_cb;

        public CouponViewHolder(ViewGroup parent) {
            super(parent, R.layout.shop_card_item);
            iv_cb = $(R.id.iv_cb);
            ll_cb = $(R.id.ll_cb);
            tv_shopName = $(R.id.tv_shopName);
            recyclerView = $(R.id.recyclerView);
            MyLayoutManager manager = new MyLayoutManager(mContext);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new RecycleViewDivider(
                    mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0), mContext.getResources().getColor(R.color.transparent)));
            recyclerView.setLayoutManager(manager);
        }

        @Override
        public void setData(final CartM.DataBean.CartsBean bean, final int position) {
            String shop_name = bean.getShop_name();
            if (!Tools.isNull(shop_name)) {
                tv_shopName.setText(shop_name);
            } else {
                tv_shopName.setText("暂无店铺");
            }
            if (bean.isChecked()) {
                iv_cb.setImageResource(R.mipmap.select_sel_1x);
            } else {
                iv_cb.setImageResource(R.mipmap.select_def_1x);
            }
            Log.i("大类是否选中", bean.isChecked() + "");
            final List<CartM.DataBean.CartsBean.GoodsBean> goods = bean.getGoods();
            String total_price = bean.getTotal_price();// 当下购物车的总价格
            adapter = new ShopCardItemAdapter(mContext,position, goods, onRefresh, onOrderNum, total_price);
            recyclerView.setAdapter(adapter);
            ll_cb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 父类选中 下面子类也设为全部选中,反之
                    if (bean.isChecked()) {
                        bean.setChecked(false);
                    } else {
                        bean.setChecked(true);
                    }
                    for (CartM.DataBean.CartsBean.GoodsBean item : goods) {
                        if (bean.isChecked()) {
                            item.setIs_check(1);
                        } else {
                            item.setIs_check(0);
                        }
                    }
                    adapter.notifyDataSetChanged();
                    notifyDataSetChanged();
                    onActivityRefresh.onActivityRefresh(bean.isChecked(), "-1", position);
                }
            });
        }
    }

    /**
     * 刷新购物车数量和价格
     */
    public ShopCardItemAdapter.OnOrderNum onOrderNum = new ShopCardItemAdapter.OnOrderNum() {
        @Override
        public void onOrderNumber(String cid, int num) {
            onCartNumber.onCartNumber(cid, num);
        }
    };


    /**
     * 刷新选中状态
     */
    public ShopCardItemAdapter.OnRefresh onRefresh = new ShopCardItemAdapter.OnRefresh() {
        @Override
        public void onRefresh(int index, boolean isChecked, String cid) {
            int a = 0;// 是否有选中的
            int b = 0;
            CartM.DataBean.CartsBean bean = list.get(index);// 取出点击大类的item
            List<CartM.DataBean.CartsBean.GoodsBean> goods = bean.getGoods();
            for (CartM.DataBean.CartsBean.GoodsBean item : goods) {
                // 循环子类是否全部选中
                if (item.getIs_check() == 1) {
                    a = 1;
                } else {
                    b = 1;
                }
                if (a == 1 && b == 0) {
                    // 子类全部选中
                    bean.setChecked(true);
                } else if (a == 0 && b == 1) {
                    bean.setChecked(false);
                } else if (a == 1 && b == 1) {
                    bean.setChecked(false);
                }
            }
            onActivityRefresh.onActivityRefresh(isChecked, cid, index);
            notifyDataSetChanged();
        }
    };

    /**
     * 刷新子类适配器
     */
    public void onRefreshAdapter() {
        if (adapter != null)
            adapter.notifyDataSetChanged();
    }
}
